package com.dianping.cat.consumer.business.model.transform;

import com.dianping.cat.consumer.business.model.IEntity;
import com.dianping.cat.consumer.business.model.entity.BusinessItem;
import com.dianping.cat.consumer.business.model.entity.BusinessReport;
import com.dianping.cat.consumer.business.model.entity.Segment;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;




public class DefaultSaxParser
  extends DefaultHandler
{
/*  30 */   private DefaultLinker m_linker = new DefaultLinker(true);

  private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  34 */   private Stack<String> m_tags = new Stack();

  private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  40 */   private StringBuilder m_text = new StringBuilder();


/*  43 */   public static BusinessReport parse(InputStream in) throws SAXException, IOException { return (BusinessReport)parseEntity(BusinessReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  47 */   public static BusinessReport parse(Reader reader) throws SAXException, IOException { return (BusinessReport)parseEntity(BusinessReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  51 */   public static BusinessReport parse(String xml) throws SAXException, IOException { return (BusinessReport)parseEntity(BusinessReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  57 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  58 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  60 */       factory.setValidating(false);
/*  61 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  63 */       factory.newSAXParser().parse(is, handler);
/*  64 */       return (T)handler.getEntity();
/*  65 */     } catch (ParserConfigurationException e) {
/*  66 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  71 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  75 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  81 */     if (obj instanceof String) {
/*  82 */       String str = (String)obj;

/*  84 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  85 */         return (T)str.substring(1);
      }
/*  87 */       return obj;
    }
/*  89 */     if (obj instanceof InputStream) {
/*  90 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  92 */       in.mark(3);

/*  94 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/*  95 */         in.reset();
      }

/*  98 */       return (T)in;
/*  99 */     }  if (obj instanceof Reader) {
/* 100 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 102 */       in.mark(1);

/* 104 */       if (in.read() != 65279) {
/* 105 */         in.reset();
      }

/* 108 */       return (T)in;
    }
/* 110 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 116 */     if (value == null || value.length() == 0) {
/* 117 */       return defaultValue;
    }

/* 120 */     if (type == Boolean.class)
/* 121 */       return (T)Boolean.valueOf(value); 
/* 122 */     if (type == Integer.class)
/* 123 */       return (T)Integer.valueOf(value); 
/* 124 */     if (type == Long.class)
/* 125 */       return (T)Long.valueOf(value); 
/* 126 */     if (type == Short.class)
/* 127 */       return (T)Short.valueOf(value); 
/* 128 */     if (type == Float.class)
/* 129 */       return (T)Float.valueOf(value); 
/* 130 */     if (type == Double.class)
/* 131 */       return (T)Double.valueOf(value); 
/* 132 */     if (type == Byte.class)
/* 133 */       return (T)Byte.valueOf(value); 
/* 134 */     if (type == Character.class) {
/* 135 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 137 */     return (T)value;
  }




/* 143 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 148 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 153 */     if (uri == null || uri.length() == 0) {
/* 154 */       this.m_objs.pop();
/* 155 */       this.m_tags.pop();
    }


/* 159 */     this.m_text.setLength(0);
  }


/* 163 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 167 */   protected String getText() { return this.m_text.toString(); }


  private void parseForBusinessItem(BusinessItem parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 171 */     if ("segment".equals(qName)) {
/* 172 */       Segment segment = this.m_maker.buildSegment(attributes);

/* 174 */       this.m_linker.onSegment(parentObj, segment);
/* 175 */       this.m_objs.push(segment);
    } else {
/* 177 */       throw new SAXException(String.format("Element(%s) is not expected under business-item!", new Object[] { qName }));
    }

/* 180 */     this.m_tags.push(qName);
  }

  private void parseForBusinessReport(BusinessReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 184 */     if ("business-item".equals(qName)) {
/* 185 */       BusinessItem businessItem = this.m_maker.buildBusinessItem(attributes);

/* 187 */       this.m_linker.onBusinessItem(parentObj, businessItem);
/* 188 */       this.m_objs.push(businessItem);
    } else {
/* 190 */       throw new SAXException(String.format("Element(%s) is not expected under business-report!", new Object[] { qName }));
    }

/* 193 */     this.m_tags.push(qName);
  }

  private void parseForSegment(Segment parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 197 */     this.m_objs.push(parentObj);
/* 198 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 202 */     if ("business-report".equals(qName)) {
/* 203 */       BusinessReport businessReport = this.m_maker.buildBusinessReport(attributes);

/* 205 */       this.m_entity = businessReport;
/* 206 */       this.m_objs.push(businessReport);
/* 207 */       this.m_tags.push(qName);
/* 208 */     } else if ("business-item".equals(qName)) {
/* 209 */       BusinessItem businessItem = this.m_maker.buildBusinessItem(attributes);

/* 211 */       this.m_entity = businessItem;
/* 212 */       this.m_objs.push(businessItem);
/* 213 */       this.m_tags.push(qName);
/* 214 */     } else if ("segment".equals(qName)) {
/* 215 */       Segment segment = this.m_maker.buildSegment(attributes);

/* 217 */       this.m_entity = segment;
/* 218 */       this.m_objs.push(segment);
/* 219 */       this.m_tags.push(qName);
    } else {
/* 221 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 227 */     if (uri == null || uri.length() == 0) {
/* 228 */       if (this.m_objs.isEmpty()) {
/* 229 */         parseRoot(qName, attributes);
      } else {
/* 231 */         Object parent = this.m_objs.peek();
/* 232 */         String tag = (String)this.m_tags.peek();

/* 234 */         if (parent instanceof BusinessReport) {
/* 235 */           parseForBusinessReport((BusinessReport)parent, tag, qName, attributes);
/* 236 */         } else if (parent instanceof BusinessItem) {
/* 237 */           parseForBusinessItem((BusinessItem)parent, tag, qName, attributes);
/* 238 */         } else if (parent instanceof Segment) {
/* 239 */           parseForSegment((Segment)parent, tag, qName, attributes);
        } else {
/* 241 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 245 */       this.m_text.setLength(0);
    } else {
/* 247 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 253 */       return (new SimpleDateFormat(format)).parse(str);
/* 254 */     } catch (ParseException e) {
/* 255 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/business/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */